home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1FYXH7Z (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  3.3 KB  |  146 lines

  1. package java.awt;
  2.  
  3. import java.io.Serializable;
  4.  
  5. public class Polygon implements Shape, Serializable {
  6.    public int npoints = 0;
  7.    public int[] xpoints = new int[4];
  8.    public int[] ypoints = new int[4];
  9.    protected Rectangle bounds = null;
  10.    private static final long serialVersionUID = -6460061437900069969L;
  11.  
  12.    public Polygon() {
  13.    }
  14.  
  15.    public Polygon(int[] xpoints, int[] ypoints, int npoints) {
  16.       this.npoints = npoints;
  17.       this.xpoints = new int[npoints];
  18.       this.ypoints = new int[npoints];
  19.       System.arraycopy(xpoints, 0, this.xpoints, 0, npoints);
  20.       System.arraycopy(ypoints, 0, this.ypoints, 0, npoints);
  21.    }
  22.  
  23.    public void addPoint(int x, int y) {
  24.       if (this.npoints == this.xpoints.length) {
  25.          int[] tmp = new int[this.npoints * 2];
  26.          System.arraycopy(this.xpoints, 0, tmp, 0, this.npoints);
  27.          this.xpoints = tmp;
  28.          tmp = new int[this.npoints * 2];
  29.          System.arraycopy(this.ypoints, 0, tmp, 0, this.npoints);
  30.          this.ypoints = tmp;
  31.       }
  32.  
  33.       this.xpoints[this.npoints] = x;
  34.       this.ypoints[this.npoints] = y;
  35.       ++this.npoints;
  36.       if (this.bounds != null) {
  37.          this.updateBounds(x, y);
  38.       }
  39.  
  40.    }
  41.  
  42.    void calculateBounds(int[] xpoints, int[] ypoints, int npoints) {
  43.       int boundsMinX = Integer.MAX_VALUE;
  44.       int boundsMinY = Integer.MAX_VALUE;
  45.       int boundsMaxX = Integer.MIN_VALUE;
  46.       int boundsMaxY = Integer.MIN_VALUE;
  47.  
  48.       for(int i = 0; i < npoints; ++i) {
  49.          int x = xpoints[i];
  50.          boundsMinX = Math.min(boundsMinX, x);
  51.          boundsMaxX = Math.max(boundsMaxX, x);
  52.          int y = ypoints[i];
  53.          boundsMinY = Math.min(boundsMinY, y);
  54.          boundsMaxY = Math.max(boundsMaxY, y);
  55.       }
  56.  
  57.       this.bounds = new Rectangle(boundsMinX, boundsMinY, boundsMaxX - boundsMinX, boundsMaxY - boundsMinY);
  58.    }
  59.  
  60.    public boolean contains(int x, int y) {
  61.       return this.inside(x, y);
  62.    }
  63.  
  64.    public boolean contains(Point p) {
  65.       return this.contains(p.x, p.y);
  66.    }
  67.  
  68.    /** @deprecated */
  69.    public Rectangle getBoundingBox() {
  70.       if (this.bounds == null) {
  71.          this.calculateBounds(this.xpoints, this.ypoints, this.npoints);
  72.       }
  73.  
  74.       return this.bounds;
  75.    }
  76.  
  77.    public Rectangle getBounds() {
  78.       return this.getBoundingBox();
  79.    }
  80.  
  81.    /** @deprecated */
  82.    public boolean inside(int x, int y) {
  83.       if (!this.getBoundingBox().inside(x, y)) {
  84.          return false;
  85.       } else {
  86.          int hits = 0;
  87.          int ySave = 0;
  88.  
  89.          int i;
  90.          for(i = 0; i < this.npoints && this.ypoints[i] == y; ++i) {
  91.          }
  92.  
  93.          for(int n = 0; n < this.npoints; ++n) {
  94.             int j = (i + 1) % this.npoints;
  95.             int dx = this.xpoints[j] - this.xpoints[i];
  96.             int dy = this.ypoints[j] - this.ypoints[i];
  97.             if (dy != 0) {
  98.                int rx = x - this.xpoints[i];
  99.                int ry = y - this.ypoints[i];
  100.                if (this.ypoints[j] == y && this.xpoints[j] >= x) {
  101.                   ySave = this.ypoints[i];
  102.                }
  103.  
  104.                if (this.ypoints[i] == y && this.xpoints[i] >= x && ySave > y != this.ypoints[j] > y) {
  105.                   --hits;
  106.                }
  107.  
  108.                float s = (float)ry / (float)dy;
  109.                if ((double)s >= (double)0.0F && (double)s <= (double)1.0F && s * (float)dx >= (float)rx) {
  110.                   ++hits;
  111.                }
  112.             }
  113.  
  114.             i = j;
  115.          }
  116.  
  117.          if (hits % 2 != 0) {
  118.             return true;
  119.          } else {
  120.             return false;
  121.          }
  122.       }
  123.    }
  124.  
  125.    public void translate(int deltaX, int deltaY) {
  126.       for(int i = 0; i < this.npoints; ++i) {
  127.          int[] var10000 = this.xpoints;
  128.          var10000[i] += deltaX;
  129.          var10000 = this.ypoints;
  130.          var10000[i] += deltaY;
  131.       }
  132.  
  133.       if (this.bounds != null) {
  134.          this.bounds.translate(deltaX, deltaY);
  135.       }
  136.  
  137.    }
  138.  
  139.    void updateBounds(int x, int y) {
  140.       this.bounds.x = Math.min(this.bounds.x, x);
  141.       this.bounds.width = Math.max(this.bounds.width, x - this.bounds.x);
  142.       this.bounds.y = Math.min(this.bounds.y, y);
  143.       this.bounds.height = Math.max(this.bounds.height, y - this.bounds.y);
  144.    }
  145. }
  146.